package com.oromwin.repos.common.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;

public class FSHelper {
    private static Log log = Logger.getLog(FSHelper.class);
    public static boolean mkdir(File file) throws ReposException {
        try {
            file.mkdirs();
            if (log.isDebugEnabled()) log.debug("Make directory '"+file.toString()+"'");
            return true;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input path is null, {"+file+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+file+"}");
        }
    }
    public static boolean ls(File file) throws ReposException {
        try {
            return file.exists();
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input path is null, {"+file+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+file+"}");
        }
    }
    public static boolean cp(File src, File dst) throws ReposException {
        FileInputStream is = null;
        FileOutputStream os = null;
        int bytesRead = 0;
        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dst);
            byte[] buffer = new byte[1024];   
            while ((bytesRead = is.read(buffer, 0, 1024)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            if (log.isDebugEnabled()) log.debug("Copy '"+src.toString()+"' '"+dst.toString()+"'");
            return true;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input or Output path is null, {"+src+":"+dst+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+src+":"+dst+"}");
        } catch (IOException e) {
            throw ReposException.create(log, e, "Failed to read or write from stream, {"+src+":"+dst+"}");
        } finally {
            try{ is.close(); }catch(IOException ioe){}
            try{ os.close(); }catch(IOException ioe){}
        }
    }
    public static boolean rm(File file) throws ReposException {
        try {
            file.delete();
            if (log.isDebugEnabled()) log.debug("Delete '"+file.toString()+"'");
            return true;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input path is null, {"+file+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+file+"}");
        }
    }
    public static boolean mv(String src, String dst) throws ReposException {
        try {
            File srcFile = new File(src);
            File dstFile = new File(dst);
            return srcFile.renameTo(dstFile);
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input or Output path is null, {"+src+":"+dst+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+src+":"+dst+"}");
        }
    }
    public static boolean mv(File src, File dst) throws ReposException {
        try {
            return src.renameTo(dst);
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input or Output path is null, {"+src+":"+dst+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+src+":"+dst+"}");
        }
    }
    public static File mkfile(String path) throws ReposException {
        File file = null;
        try {
            file = new File(path);
            file.createNewFile();
            return file;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input or Output path is null, {"+path+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+path+"}");
        } catch (IOException e) {
            throw ReposException.create(log, e, "Can't read from stream, {"+file.toString()+"}");
        }
    }
    public static boolean valid(String filename) {
        File file = null;
        try {
            file = File.createTempFile(filename, ".tmp");
        } catch (IOException e) {
            if (log.isInfoEnabled()) log.info("Can't create file with {" + filename + "}");
            return false;
        } finally {
            try {
                if (file != null) file.delete();
            } finally {}
        }
        return true;
    }
}
